<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <div>
        <span style="font-size:12px;">输入搜索条件：</span>
        <el-input class="input-name" v-model="searchParams.name" placeholder="角色名称"></el-input>
        <el-button type="primary" @click="loadRoles(1)">查询搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </div>
      <el-button @click="handleCreate">添加角色</el-button>
    </div>

    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="角色名称"> </el-table-column>
      <el-table-column prop="description" label="描述"> </el-table-column>
      <el-table-column prop="createdTime" label="添加事件"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
           <el-button type="text" @click="allocMenu(scope.row.id)">分配菜单</el-button>
           <el-button type="text" @click="handleUpdate(scope.row)">编辑</el-button><br>
           <el-button type="text" @click="allocResource(scope.row.id)">分配资源</el-button>
           <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="40%"
      @close="handleClose">
      <el-form label-width="90px" :model="roleForm">
        <el-form-item label="角色名称：">
            <el-input v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="角色编码：">
            <el-input v-model="roleForm.code"></el-input>
        </el-form-item>
        <el-form-item label="描述：">
            <el-input v-model="roleForm.description" type="textarea" :rows="4"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script lang="ts">
import Vue from 'vue'
// import DialogForm from './components/dialog-form.vue'
import { getRolePages, saveOrUpdateRole, deleteRole } from '@/services/role'

export default Vue.extend({
  name: 'RoleIndex',

  components: {
    // DialogForm
  },

  data () {
    return {
      searchParams: {
        name: '',
        current: 1,
        size: 50
      },
      tableData: [],
      dialogVisible: false,
      roleForm: {
        id: '',
        name: '',
        code: '',
        description: ''
      }
    }
  },

  mounted () {
    this.loadRoles()
  },

  methods: {
    loadRoles (current: number | void) {
      if (current) {
        this.searchParams.current = current
      }
      getRolePages(this.searchParams).then(res => {
        if (res.data.code === '000000') {
          const { records } = res.data.data
          this.tableData = records
        }
      })
    },

    reset () {
      this.searchParams = {
        name: '',
        current: 1,
        size: 50
      }
      this.loadRoles()
    },

    handleCreate () {
      this.dialogVisible = true
    },

    handleUpdate (row: any) {
      const { id, name, description, code } = row
      this.roleForm = { id, name, description, code }
      this.dialogVisible = true
    },

    submitForm () {
      saveOrUpdateRole(this.roleForm).then(res => {
        if (res.data.code === '000000') {
          this.$message.success(this.roleForm.id ? '编辑成功' : '添加成功')
          this.dialogVisible = false
          this.loadRoles()
        } else {
          this.$message.error(res.data.message)
        }
      })
    },

    handleDelete (id: number) {
      this.$confirm('确定删除?', '提示').then(() => {
        deleteRole(id).then(res => {
          if (res.data.code === '000000') {
            this.$message.success('删除成功')
            this.loadRoles()
          }
        })
      }).catch(() => {
        this.$message('已取消删除')
      })
    },

    allocMenu (id: number) {
      this.$router.push({
        name: 'alloc-menu',
        params: {
          roleId: id + ''
        }
      })
    },

    allocResource (id: number) {
      this.$router.push({
        name: 'alloc-resource',
        params: {
          roleId: id + ''
        }
      })
    },

    handleClose () {
      this.roleForm = {
        id: '',
        name: '',
        code: '',
        description: ''
      }
    }
  }
})
</script>

<style lang="scss" scoped>
.input-name{
  margin: 10px;
  width: 150px;
}
</style>
